package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    @RequiresPermissions(value = {"permission:list", "权限列表"}, logical = Logical.OR)
    public String list(Model model, QueryObject queryObject) {
        model.addAttribute("result", permissionService.list(queryObject));
        return "permission/list"; // /WEB-INF/views/permission/list.jsp
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"permission:delete", "权限删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        permissionService.delete(id);
        return new JsonResult();
    }

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reload();
            return new JsonResult("加载成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("加载失败", false);
        }
    }
}
